/*
 * reader.cpp
 *
 *  Created on: Aug 25, 2009
 *      Author: zaca
 */

#include "reader.h"

#include <memory>
#include <iostream>

namespace biosignal
{

ReaderRegistry& ReaderRegistry::get()
{
    static ReaderRegistry instance;
    return instance;
}

void ReaderRegistry::add(reader_creator creator)
{
    Reader* _ptr = creator();
    std::auto_ptr<Reader> reader(_ptr);
    m_readers.push_back(creator);
}

ReaderRegistry::iterator ReaderRegistry::begin()
{
    return m_readers.begin();
}

ReaderRegistry::iterator ReaderRegistry::end()
{ return m_readers.end(); }

const BioSignal* ReaderRegistry::load( std::string file_name)
{
	ReaderRegistry::iterator it;

	for(it=this->begin(); it!=this->end(); ++it)
	{
	    reader_creator func = *it;
	    Reader* _ptr = func();
	    std::auto_ptr<Reader> reader(_ptr);
		if(reader->can_read(file_name))
		{
			return reader->read(file_name);
		}

	}
	std::cout << "not found reader for file: " << file_name << std::endl;
	return NULL;
}

ReaderRegistration::ReaderRegistration(reader_creator creator)
{
    Reader* _ptr = creator();
    std::auto_ptr<Reader> reader(_ptr);
    ReaderRegistry::get().add(creator);
}


}
